package Chocolatier;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import java.util.StringTokenizer;

import BoiteDeChocolat.BoiteDeChocolat;
import ChampionFactory.AbstractChampionFactory;

public final class ConditionneurChocolatier extends Chocolatier implements Observer
{
	private static volatile ConditionneurChocolatier instance = null;
	private HashSet<String> listeMagasins; // on evite les doublons
	private boolean special;
	private BigDecimal coeff;

	private ConditionneurChocolatier()
	{
		super();
		listeMagasins = new HashSet<String>();
		coeff = new BigDecimal("1.00");
	}

	public boolean isSpecial() {
		return special;
	}

	public void setSpecial(boolean special) {
		this.special = special;
	}

	public BigDecimal getCoeff() {
		return coeff;
	}

	public void setCoeff(BigDecimal coeff) {
		this.coeff = coeff;
	}

	private ConditionneurChocolatier(HashSet<String> listeMagasins)
	{
		super();
		this.listeMagasins = listeMagasins;
		coeff = new BigDecimal("1.00");
	}

	/**
	 * Singleton pattern - Ensure that there is only one instance of
	 * ConditionneurChocolatier
	 * 
	 * @return an unique ConditionneurChocolatier if it is the first
	 *         instantiation, otherwise return the already existing instance
	 */
	public final static ConditionneurChocolatier getInstance()
	{
		if (ConditionneurChocolatier.instance == null)
		{
			// In case of multithread running
			synchronized (ConditionneurChocolatier.class)
			{
				if (ConditionneurChocolatier.instance == null)
					ConditionneurChocolatier.instance = new ConditionneurChocolatier();
			}
		}
		return ConditionneurChocolatier.instance;
	}

	public HashSet<String> getListeMagasins()
	{
		return listeMagasins;
	}

	public void setListeMagasins(HashSet<String> listeMagasins)
	{
		this.listeMagasins = listeMagasins;
	}

	public void addMagasin(String magasin)
	{
		this.listeMagasins.add(new String(magasin));
	}

	public void supprMagasin(String magasin)
	{
		this.listeMagasins.remove(magasin);
	}
	
	public void ajouterPlateau(BoiteDeChocolat b){
		b.afficheDescription();
		
	}
	
	public BigDecimal determinerPrix(BoiteDeChocolat b,int taille){
		if(taille==3){
			return b.calculerCout().multiply(new BigDecimal("1.20")).multiply(coeff).setScale(2, RoundingMode.CEILING);	
		}
		if(taille==6){
			return b.calculerCout().multiply(new BigDecimal("1.10")).multiply(coeff).setScale(2, RoundingMode.CEILING);
		}
		if(taille==9){
			return b.calculerCout().multiply(new BigDecimal("1.05")).multiply(coeff).setScale(2, RoundingMode.CEILING);
		}
		else
			return new BigDecimal("0.00");	
	}
	


	@Override
	public void update(Observable obs, Object arg1) {
		if(obs instanceof AbstractChampionFactory){
			Calendar calendar=new GregorianCalendar(Locale.FRANCE);
			Date date=calendar.getTime();
			System.out.println("Notification recu le "+new SimpleDateFormat().format(date));
			AbstractChampionFactory a = (AbstractChampionFactory) obs;
			special = a.getSaison();
			if(special)
				coeff = new BigDecimal("1.10");
			else
				coeff = new BigDecimal("1.00");
		}	
	}
	
	public boolean checkSaveur(String b){
		//System.out.println("ici");
		Map<String,Integer> res = new HashMap<String,Integer>();
		StringTokenizer st = new StringTokenizer(b.toString(),"-");
		while(st.hasMoreElements()){
			String sb = new String(st.nextToken());
			if(sb.startsWith(" oeuf")){
				String stock []= sb.split("\n");
				if(!res.containsKey(stock[0]))
					res.put(stock[0], new Integer(1));
				else
					res.put(stock[0],res.get(stock[0]) +1);
			}
		}
		//System.out.println(res);
		if (res.size() == 3) {
			
			Object saveurList[] =  res.keySet().toArray();
			return (res.get(saveurList[0]).equals(res.get(saveurList[1])) && (res
					.get(saveurList[1]).equals(res.get(saveurList[2]))));
		} else if (res.size() == 1)
			return true;
		else
			return false;

	}

}
